package com.comsoftstar.nt5s.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hzc.nt5s.R;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Martin on 2017/1/18.
 */

public class PickAdapter extends BaseAdapter {

  private LayoutInflater layoutInflater;
  private List<String> data = new ArrayList<String>();
  private boolean isShowDelete;

  public PickAdapter(Context context) {
    layoutInflater = LayoutInflater.from(context);
  }

  public void setNewData(List<String> data) {
    this.data.clear();
    if (data != null) {
      this.data.addAll(data);
    }
    notifyDataSetChanged();
  }
  public void setIsShowDelete(boolean isShowDelete) {
    this.isShowDelete = isShowDelete;
    notifyDataSetChanged();
  }


  @Override
  public int getCount() {
    return data.size();
  }

  @Override
  public Object getItem(int position) {
    return data.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View view;
    if (convertView == null) {
      view = layoutInflater.inflate(R.layout.image_item, null);
      holder = new ViewHolder();
      holder.imageView = (ImageView) view.findViewById(R.id.image);
      holder.deleteView= (ImageView) view.findViewById(R.id.delete_markView);

      view.setTag(holder);
    } else {
      view=convertView;
      holder = (ViewHolder) view.getTag();
    }
    holder.deleteView.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);
    holder.imageView.setImageBitmap(getLoacalBitmap(data.get(position)));
    return view;
  }
  public static Bitmap getLoacalBitmap(String url) {
    try {
      FileInputStream fis = new FileInputStream(url);
      return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片

    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return null;
    }
  }

  class ViewHolder {
    public ImageView imageView,deleteView;
  }
}
